Ext.define("App.targetconfiguration.view.TargetConfigurationEditor", {
    extend: 'Ext.panel.Panel',
    alias: ['widget.TargetConfigurationEditor'],
    controller: 'TargetConfigurationEditorController',
    scrollable: false,
    layout: 'border',
    buttons: {
        style: 'background:#f1f1f1;',
        items: ['->', {
            text: '保存',
            handler: function () {
                this.up('TargetConfigurationEditor').save();
            }
        }, {
            text: '重置',
            handler: function () {
                this.up('TargetConfigurationEditor').reset();
            }
        }]
    },
    save: function () {
        var me = this;
        me.down('tabpanel').getActiveTab().submit();
    },
    listeners: {
        boxready: 'onBoxready',
        save: 'onSaved'
    },
    items: [
        {
            xtype: 'tabpanel',
            region: 'center',
            itemId: 'collectionGroupTab',
            columnWidth: 1, padding: 0, margin: 0,
            items: []
        }
    ]
})

Ext.define('App.targetconfiguration.view.TargetConfigurationEditorController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.TargetConfigurationEditorController',
    onBoxready: function () {
        var me = this;
        me.loadReportConfigurations();
    },
    loadReportConfigurations: function () {
        var me = this;
        var view = this.getView();
        if (Ext.isEmpty(app.project) || Ext.isEmpty(app.project.id)) {
            return;
        }
        view.mask('加载...')
        Ext.Ajax.request({
            url: 'project/edit',
            params: {id: app.project.id},
            success: function (resp) {
                if (resp.result.success == true) {
                    var projectReportCfgs = resp.result.data.projectReportConfigurationReferences;
                    var iterationReportCfgs = resp.result.data.iterationReportConfigurationReferences;
                    var array = new Array();
                    for (var i = 0; i < iterationReportCfgs.length; i++) {
                        if (iterationReportCfgs[i].reportConfiguration.name != '关键指标') {
                            array.push(iterationReportCfgs[i]);
                        }
                    }
                    for (var i = 0; i < projectReportCfgs.length; i++) {
                        array.push(projectReportCfgs[i]);
                    }
                    me.createReportPanels(array);
                }
                view.unmask();
            },
            failure: function () {
                view.unmask();
            }
        });
    },
    createReportPanels: function (array) {
        var me = this;
        var tab = me.getView().down('[itemId="collectionGroupTab"]');
        var i = 0;
        for (; i < array.length; i++) {
            var cfg = array[i];
            tab.insert(i, Ext.create('Extend.form.Panel', {
                url: 'target_configuration/save_list',
                entity: 'entity',
                resetBySave: false,
                scrollable: true,
                title: cfg.reportConfiguration.name,
                layout: 'column',
                reportConfiguration: cfg.reportConfiguration,
                margin: '10 0 10 0',
                listeners: {
                    boxready: function () {
                        this.ready = true;
                        me.initKpiConfigurationFields(this);
                    },
                    activate: function () {
                        if (this.ready == true) {
                            this.removeAll();
                            me.initKpiConfigurationFields(this);
                        }
                    }
                }
            }));
        }
        tab.setActiveItem(0);
    },
    initKpiConfigurationFields: function (view) {
        var me = this;
        var reportConfiguration = view.reportConfiguration;
        view.mask('加载...')
        Ext.Ajax.request({
            url: 'target_configuration/load_target_configuration',
            params: {
                'reportConfigurationId': reportConfiguration.id,
                'projectId': app.project.id
            },
            success: function (resp) {
                if (resp.result.success == true) {
                    var data = resp.result.data;
                    for (var i = 0; i < data.length; i++) {
                        var targetConfiguration = data[i];
                        me.createForm(view, reportConfiguration, targetConfiguration, i);
                    }
                }
                view.unmask();
            },
            failure: function () {
                view.unmask();
            }
        });
    },
    createForm: function (view, reportConfiguration, targetConfiguration, idx) {
        var title = "<h3>" + targetConfiguration.kpiName + "</h3>";
        var container = Ext.create({
            xtype: 'fieldset',
            columnWidth: 0.5,
            title: title,
            layout: 'column',
            margin: '10,10,10,10'
        });
        var idField = Ext.create({
            xtype: 'hidden',
            name: 'targetConfigurations[' + idx + '].id',
            value: targetConfiguration.id
        });
        container.add(idField);

        view.add(idField);
        var projectField = Ext.create({
            xtype: 'hidden',
            name: 'targetConfigurations[' + idx + '].projectId',
            value: targetConfiguration.projectId
        });
        container.add(projectField);
        var configurationId = Ext.create({
            xtype: 'hidden',
            name: 'targetConfigurations[' + idx + '].reportConfigurationId',
            value: targetConfiguration.reportConfigurationId
        });
        container.add(configurationId);
        var configurationId = Ext.create({
            xtype: 'hidden',
            name: 'targetConfigurations[' + idx + '].kpiConfigurationReference.id',
            value: targetConfiguration.kpiConfigurationReference.id
        });
        container.add(configurationId);
        var baseField = Ext.create({
            xtype: 'numberfield',
            fieldLabel: '基线值',
            name: 'targetConfigurations[' + idx + '].baseLineValue',
            labelWidth: 50,
            margin: '10 10 25 10',
            columnWidth: 0.25,
            value: targetConfiguration.baseLineValue
        });
        container.add(baseField);
        var target = Ext.create({
            xtype: 'numberfield',
            fieldLabel: '目标值',
            name: 'targetConfigurations[' + idx + '].targetValue',
            labelWidth: 50,
            margin: '10 10 25 10',
            columnWidth: 0.25,
            value: targetConfiguration.targetValue
        });
        container.add(target);
        var upValue = Ext.create({
            xtype: 'numberfield',
            fieldLabel: '上限值',
            columnWidth: 0.25,
            margin: '10 10 25 10',
            labelWidth: 50,
            name: 'targetConfigurations[' + idx + '].maxValue',
            value: targetConfiguration.maxValue
        });
        container.add(upValue);
        var downValue = Ext.create({
            xtype: 'numberfield',
            fieldLabel: '下限值',
            labelWidth: 50,
            margin: '10 10 25 10',
            name: 'targetConfigurations[' + idx + '].minValue',
            columnWidth: 0.25,
            value: targetConfiguration.minValue
        });
        container.add(downValue);
        view.add(container);
    },
    onSaved: function () {
        Extend.Msg.info('提示', '指标目标配置信息已经保存成功成功！');
    }
})